﻿namespace PropOnline.Controllers
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using PropOnline.Models;
    using PropOnline.Security;

    public class AccountController : Controller
    {
        public ActionResult LogOn(string refresh)
        {
            var model = new AutenticacionModel();
            if (!string.IsNullOrWhiteSpace(refresh) && this.Request.Cookies[Autenticacion.IdUsuarioCookieName] == null)
            {
                model.UsuarioId = Autenticacion.CrearIdUsuario();

                var idUsuarioCookie = new HttpCookie(Autenticacion.IdUsuarioCookieName, model.UsuarioId);
                idUsuarioCookie.Path = this.Request.Path;
                idUsuarioCookie.Expires = DateTime.Now.AddYears(50);
                this.Response.Cookies.Add(idUsuarioCookie);
            }
            else if (this.Request.Cookies[Autenticacion.IdUsuarioCookieName] != null)
            {
                model.UsuarioId = this.Request.Cookies[Autenticacion.IdUsuarioCookieName].Value;
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult LogOn(AutenticacionModel autenticacionModel, string returnUrl)
        {
            int codigo = -1;
            int idUsuario = -1;

            if (this.Request.Cookies[Autenticacion.IdUsuarioCookieName] == null ||
                autenticacionModel == null ||
                string.IsNullOrWhiteSpace(autenticacionModel.CodigoAutenticacion) ||
                autenticacionModel.CodigoAutenticacion.Length != 6 ||
                !int.TryParse(autenticacionModel.CodigoAutenticacion, out codigo) ||
                !int.TryParse(this.Request.Cookies[Autenticacion.IdUsuarioCookieName].Value, out idUsuario) ||
                !Autenticacion.ValidarCodigo(idUsuario, codigo))
            {
                return this.RedirectToAction("LogOn");
            }

            System.Web.Security.FormsAuthentication.SetAuthCookie(this.Request.Cookies[Autenticacion.IdUsuarioCookieName].Value, true);
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpGet]
        public ActionResult evercookie_cache_page()
        {
            if (this.Request.Cookies["evercookie_cache"] == null)
            {
                return new HttpStatusCodeResult(304, "Not Modified");
            }

            this.Response.ContentType = "text/html";
            this.Response.Headers.Add("Last-Modified", DateTime.Now.ToString("R"));
            this.Response.Headers.Add("Expires", DateTime.Now.AddYears(30).ToString("R"));
            this.Response.Headers.Add("Cache-Control", "private, max-age=630720000");
            return this.Content(this.Request.Cookies["evercookie_cache"].Value);
        }

        [HttpGet]
        public ActionResult evercookie_etag_page()
        {
            if (this.Request.Cookies["evercookie_etag"] == null)
            {
                return this.Content(this.Request.Headers["If-None-Match"]);
            }

            this.Response.Headers.Add("Etag", this.Request.Cookies["evercookie_etag"].Value);
            return this.Content(this.Request.Cookies["evercookie_etag"].Value);
        }

        [HttpGet]
        public ActionResult evercookie_png_page()
        {
            if (this.Request.Cookies["evercookie_png"] == null)
            {
                return new HttpStatusCodeResult(304, "Not Modified");
            }

            var x = 200;
            var y = 1;
            var gd = new System.Drawing.Bitmap(x, y);
            var data_arr = this.Request.Cookies["evercookie_png"].Value.ToCharArray();

            x = 0;
            y = 0;
            for (var i = 0; i < data_arr.Length; i += 3)
            {
                var color = System.Drawing.Color.FromArgb(Convert.ToInt32(data_arr[i]), Convert.ToInt32(data_arr[i + 1]), Convert.ToInt32(data_arr[i + 2]));
                gd.SetPixel(x++, y, color);
            }
 
            this.Response.ContentType = "image/png";
            this.Response.Headers.Add("Last-Modified", DateTime.Now.ToString("R"));
            this.Response.Headers.Add("Expires", DateTime.Now.AddYears(30).ToString("R"));
            this.Response.Headers.Add("Cache-Control", "private, max-age=630720000");

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            gd.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
            return File(ms.ToArray(), "image/png");    
        }
    }
}
